package widget.common.managers
{
	import com.rworld.common.model.EventModel;
	import com.rworld.common.utils.MyLog;
	
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.utils.Dictionary;
	
	/**
	 *全局事件管理器。
	 * 此管理器仅可使用在 模块 与 模块之间。 
	 * @author hwust
	 * 
	 */	
	public class EventManager implements IManager
	{
		//单例
		private static var instance:EventManager = null;
		
		//广播事件的主对象
		private var mainDispather:EventDispatcher = null;
		
		//保存所有添加过监听的2维数组 [type][function]
		private var listenerList:Dictionary = null;
		
		public function EventManager()
		{
			if(instance)
			{
				throw new Error("single class by EventManager")
			}
			this.init();
		}
		
		public static function getInstance():EventManager
		{
			if(!instance)
			{
				instance = new EventManager();
			}
			return instance;
		}
		private function init():void
		{
			this.mainDispather = new EventDispatcher();
			
			listenerList = new Dictionary();
		}
		/**
		 *添加监听事件
		 *  如果 isSingle 为 TRUE 则先清除 type 类型下 的所有监听 再添加该类型 的listener 监听。
		 * @param type
		 * @param listener
		 * @param isSingle 
		 * 
		 */		
		public function addEventListener(type:String,listener:Function,isSingle:Boolean = false):void
		{
			if(isSingle)
			{
				this.removeEventListener(type);
			}
			var tempArray:Array = this.listenerList[type];
			if(tempArray)
			{
				var tempLength:uint = tempArray.length;
				for(var i:int = 0; i < tempLength; i++)
				{
					if(listener == tempArray[i])
					{
						MyLog.debug("该类型已被监听，请不要重复监听  :" + type + "  " + listener + "  " + isSingle)
						return;
					}
				}
			}
			else
			{
				tempArray = new Array();
				this.listenerList[type] = tempArray;
			}
			this.mainDispather.addEventListener(type,listener);
			tempArray.push(listener);
		}
		/**
		 * 删除监听，如果listener为null 则删除 所有 TPYE 类型下 的监听
		 * 反之删除指定监听
		 * @param type
		 * @param listener
		 * 
		 */		
		public function removeEventListener(type:String,listener:Function = null):void
		{
			if(listener == null)
			{
				this.clearAllTag(type);
			}
			else
			{
				
				this.clearTag(type,listener);
			}
		}
		public function dispatchEvent(type:String,data:EventModel):void
		{
			var tempEvent:NetStatusEvent = new NetStatusEvent(type,false,false,data);
			
			this.mainDispather.dispatchEvent(tempEvent);
		}
		private function clearAllTag(type:String):void
		{
			var tempArray:Array = this.listenerList[type];
			if(tempArray)
			{
				var tempLength:uint = tempArray.length;
				for(var i:int = 0; i < tempLength; i++)
				{
					var listener:Function = tempArray[i];
					try
					{
						this.mainDispather.removeEventListener(type,listener);
					}
					catch(e:Error)
					{
						MyLog.debug("删除监听失败，删除目标："+ type );
					}
				}
				tempArray.splice(0);
			}
			
		}
		private function clearTag(type:String,listener:Function):void
		{
			var tempArray:Array = this.listenerList[type];
			if(tempArray)
			{
				var tempLength:uint = tempArray.length;
				for(var i:int = 0; i < tempLength; i++)
				{
					if(listener == tempArray[i])
					{
						this.mainDispather.removeEventListener(type,listener);
						tempArray.splice(i,1);
						return;
					}
				}
			}
			MyLog.debug("未找到该监听 删除监听失败，删除目标："+ type);
		}
		public function dispose():void
		{
			
			mainDispather = null;
		}
		
	}
}